
package com.dz.common.core.handler;


import com.dz.common.base.BaseApiService;
import com.dz.common.base.ResponseBase;
import com.dz.common.core.exception.NotFoundDataException;
import com.netflix.hystrix.exception.HystrixRuntimeException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * 全局的的异常处理器
 */
@Slf4j
@RestControllerAdvice(value = "com.dz")
public class GlobalExceptionHandler extends BaseApiService {

    /**
     * 全局异常信息
     */
    private static final String GLOBAL_ERROR_MESSAGE = "全局异常信息 ex={}";

    /**
     * 网络忙信息
     */
    private static final String NETWORK_BUSY_MESSAGE = "网络忙，请稍后重试！";

    /**
     * 全局异常.
     *
     * @param e the e
     * @return R
     */
    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseBase exceptionHandler(Exception e) {
        return setResultError(NETWORK_BUSY_MESSAGE);
    }


    @ExceptionHandler(HystrixRuntimeException.class)
    public ResponseBase FeignExceptionHandler(HystrixRuntimeException e) {
        log.error(e.getMessage());
        return setResultError(NETWORK_BUSY_MESSAGE);
    }
    /**
     * 全局异常.
     *
     * @param e the e
     * @return R
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public ResponseBase maxUploadSizeExceptionHandler(MaxUploadSizeExceededException e) {
        return setResultError("文件不能大于10M");
    }


    /**
     * 全局异常.
     *
     * @param e the e
     * @return R
     */
/*
    @ExceptionHandler(JWTDecodeException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result<String> JWTDecodeExceptionHandler(JWTDecodeException e) {
        log.error(GLOBAL_ERROR_MESSAGE, e.getMessage(), e);

        return Result.fail("账号解析错误，请尝试重新登录。");
    }
*/

    /**
     * 全局异常.
     *
     * @param e the e
     * @return R
     */
/*
    @ExceptionHandler(TokenExpiredException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result<String> TokenExpiredExceptionHandler(TokenExpiredException e) {
        log.error(GLOBAL_ERROR_MESSAGE, e.getMessage(), e);

        return Result.fail("登录令牌信息异常，请重新登录。");
    }

*/



    /**
     * 业务异常（数据不存在）
     *
     * @param e NotFoundDataException
     * @return Result
     */
    @ExceptionHandler(NotFoundDataException.class)
    public String NotFoundDataExceptionHandler(NotFoundDataException e) {
        log.error("业务异常（数据不存在）信息 ex={}", e.getMessage(), e);
        if (e.getArgs() != null) {
            return e.getMessage()+e.getArgs();
        } else {
            return e.getMessage();
        }
    }

}
